package com.scnu409.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.scnu409.mimicontact.LatestContents;
import com.scnu409.mimicontact.R;
import com.scnu409.mimicontact.UserInf;
import com.scnu409.utils.Person;
import com.scnu409.utils.TestContent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.ViewDragHelper.Callback;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class HistoryContentFragment extends ListFragment{
	
	private Callbacks mCallbacks;
	/*
	 * 此Fragment所在的activity必须实现这个接口
	 */
	public interface Callbacks{
		public void onItemSelected(Integer id);
	}
	
	
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
		List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
		
		for(int i=0;i<TestContent._name.length;i++){
			Map<String,Object> listItem=new HashMap<String,Object>();
			listItem.put("userImg", TestContent._simpleImage[i]);
			listItem.put("userName", TestContent._name[i]);
			listItem.put("latestContent", TestContent._content[i]);
			list.add(listItem);
		}
		
		
		//@param SimpleAdapter 实现列表项
		SimpleAdapter adapter=new SimpleAdapter(getActivity(),list,R.layout.history_view_dialog,
				new String[] {"userImg","userName","latestContent"},
				new int []{R.id.user_img,R.id.user_name,R.id.latest_content});
		
		setListAdapter(adapter);
	}
	
	@Override
	public void onAttach(Activity activity){
		super.onAttach(activity);
		//如果没有实现这个接口，抛出异常
		if(!(activity instanceof Callbacks)){
			throw new IllegalStateException("hehe");
		}
		mCallbacks = (Callbacks) activity;
	}
	
	@Override
	public void onDetach(){
		super.onDetach();
		mCallbacks=null;
	}
	
	@Override
	public void onListItemClick(ListView listView,View view,int position,long id){
		super.onListItemClick(listView, listView, position, id);
		//mCallbacks.onItemSelected(id);
		Intent intent =new Intent(getActivity(),UserInf.class);
		Bundle bundle=new Bundle();
		Person person=new Person(TestContent._simpleImage[position],TestContent._name[position]);
		bundle.putSerializable("person", person);
		intent.putExtras(bundle);
		startActivity(intent);
		
	}
	
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState){
		super.onActivityCreated(savedInstanceState);
	}
}


